package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.Util.CosResultModel;
import com.be.beadmin.product.dto.input.ScheduleDto;
import com.be.beadmin.product.dto.WorkOrderProductPlanDto;
import com.be.beadmin.product.dto.input.ProductManufactDto;
import com.be.beadmin.product.dto.input.ProductManufactInforDto;
import com.be.beadmin.product.dto.output.ProductManufactInforEx;
import com.be.beadmin.product.service.ProductManufactInforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author zjm
 * @date 2023-03-19
 **/
@Api(tags = {"生产工序"})
@RestController
public class ProductManufactInforController {

    @Autowired
    private ProductManufactInforService productManufactInforService;

    @PostMapping(value = "/product/getProductManufactInforByPage")
    public ResultModel getProductManufactInforByPage(@RequestBody PageEntity pageEntity) {
        return ResultModel.success(productManufactInforService.getProductManufactInforByPage(pageEntity));
    }

    @ApiOperation("分页查询生产计划")
    @PostMapping(value = "/product/getProductManufactByPage")
    public ResultModel getProductManufactByPage(HttpServletRequest request, @RequestBody PageEntity pageEntity) {
        return ResultModel.success(productManufactInforService.getProductManufactByPage(request, pageEntity));
    }

    @ApiOperation("分页查询所有生产工序")
    @PostMapping(value = "/product/getProductManufactInforExecuteByPage")
    public ResultModel getProductManufactInforExecuteByPage(HttpServletRequest request, @RequestBody PageEntity pageEntity) {
        return ResultModel.success(productManufactInforService.getProductManufactInforExecuteByPage(request, pageEntity));
    }

//    @ApiOperation("分页查询所有生产工序By工单编号")
//    @PostMapping(value = "/product/getProductManufactInforExecuteByPageAndWo")
//    public ResultModel getProductManufactInforExecuteByPageAndWo(HttpServletRequest request, @RequestBody PageEntity pageEntity) {
//        return ResultModel.success(productManufactInforService.getProductManufactInforExecuteByPageAndWo(request, pageEntity , wo));
//    }

    @ApiOperation("根据工单ID获取该工单下的所有生产工序")
    @PostMapping(value = "/product/getProductManufactExecInfosByWoId")
    public ResultModel getProductManufactExecInfosByWoId(@ApiParam(value = "工单Id") @RequestParam String woId) {
        return ResultModel.success(productManufactInforService.getProductManufactExecInfosByWoId(woId));
    }


    @ApiOperation("根据工单Id工单下的所有生产排程计划")
    @PostMapping(value = "/product/getProductPlansByWoId")
    public ResultModel getProductPlansByWoId(@ApiParam(value = "工单Id") @RequestParam String woId) {
        return ResultModel.success(productManufactInforService.getProductPlansByWoId(woId));
    }

//    @ApiOperation("获取所有生产排程计划")
//    @PostMapping(value = "/product/getProductPlanAll")
//    public ResultModel getProductPlanAll(@RequestParam PageEntity pageEntity) {
//        return ResultModel.success(productManufactInforService.getProductPlanAll(pageEntity));
//    }


    @ApiOperation("暂停执行工序")
    @PostMapping(value = "/product/pauseProductManufactExec")
    public ResultModel pauseProductManufactExec(HttpServletRequest request, @ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {
//        try {
            int re = productManufactInforService.pauseProductManufactExec(request,productManufactInforId);
            if (re == 0) {
                return ResultModel.success("工序暂停执行");
            } else if (re == -1) {
                return ResultModel.error(re, "工序已经暂停无法再次暂停");
            }
            return ResultModel.success();
//        } catch (
//                Exception e) {
//            return ResultModel.error(-1, e.getMessage());
//        }
    }

    @ApiOperation("获取工序的状态")
    @PostMapping(value = "/product/getProductManufactStateById")
    public ResultModel getProductManufactStateById(HttpServletRequest request,@ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {
                return CosResultModel.success(productManufactInforService.getProductManufactStateById(request,productManufactInforId));


    }

//    @ApiOperation("计时开始")
//    @PostMapping(value = "/product/startTiming")
//    public ResultModel startTiming(@ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {
//        int re = productManufactInforService.startTiming(userEx, productManufactInforId);
//        if (re == 0) return ResultModel.success("计时开始");
//        if (re == -1) return ResultModel.error(re, "正在计时，请勿再次点击");
//        if (re == -2) return ResultModel.error(re, "计时结束，无法计时");
//        return ResultModel.success();
//    }

    @ApiOperation("获取该工序的实际计时时间(单位:分钟)")
    @PostMapping(value = "/product/getManuTime")
    public ResultModel getManuTime(@ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {

        return ResultModel.success(productManufactInforService.getManuTime(productManufactInforId));
    }

    @ApiOperation("计时结束")
    @PostMapping(value = "/product/pauseTiming")
    public ResultModel pauseTiming(@ApiParam(value = "工序执行的id") @RequestParam String productManufactInforId) {
        int re = productManufactInforService.pauseTiming(productManufactInforId);
        if (re == 0) return ResultModel.success("计时结束");
        if (re == -1) return ResultModel.error(re, "计时暂停中，请勿再次暂停");
        if (re == -2) return ResultModel.error(re, "计时结束，无法计时");
        return ResultModel.success();
    }


//    @ApiOperation("分页查询生产工序执行状况")
//    @PostMapping(value = "/product/getProductManufactExecByPage")
//    public ResultModel getProductManufactExecByPage(@RequestBody PageEntity pageEntity)  {
//        return ResultModel.success(productManufactInforService.getProductManufactExecByPage(pageEntity)) ;
//    }


    @PostMapping("/product/addProductManufactInfor")
    public ResultModel addProductManufactInfor(@RequestBody ProductManufactInforDto productManufactInforDto) {
        productManufactInforService.addProductManufactInfor(productManufactInforDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delProductManufactInfor")
    public ResultModel delProductManufactInfor(@RequestBody ProductManufactInforDto productManufactInforDto) {
        productManufactInforService.delProductManufactInfor(productManufactInforDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

//    @ApiOperation("更新生产时间")
//    @PostMapping("/product/updateProductManufactInfor")
//    public ResultModel updateProductManufactInfor(@RequestBody ProductManufactInforUpdateDto productManufactInforUpdateDto){
//        productManufactInforService.updateProductManufactInfor(productManufactInforUpdateDto);
//        return ResultModel.success(ResultModel.UPDATE);
//    }

//    @ApiOperation("更新生产时间-所有时间一起更新")
//    @PostMapping("/product/updateProductManufactInforAll")
//    public ResultModel updateProductManufactInforAll(@RequestBody List<ProductManufactInforUpdateDto> productManufactInforUpdateDtos){
//        productManufactInforService.updateProductManufactInforAll(productManufactInforUpdateDtos);
//        return ResultModel.success(ResultModel.UPDATE);
//    }

    @ApiOperation("更新生产时间")
    @PostMapping("/product/updateProductManufactTime")
    public ResultModel updateProductManufactInforAll(@RequestBody ProductManufactDto productManufactDto) {
        productManufactInforService.updateProductManufactTime(productManufactDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

//    @ApiOperation("手动排程")
//    @PostMapping("/product/manualSchedule")
//    public ResultModel manualSchedule(@RequestBody WorkOrderProductPlanDto workOrderProductPlanDto) {
//        productManufactInforService.manualSchedule(workOrderProductPlanDto);
//        return ResultModel.success(ResultModel.UPDATE);
//    }

    @ApiOperation("计划变更")
    @PostMapping("/product/updateProductPlanTime")
    public ResultModel updateProductPlanTime(@RequestBody WorkOrderProductPlanDto workOrderProductPlanDto) {
        productManufactInforService.updateProductPlanTime(workOrderProductPlanDto,false);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("自动排程By工单id")
    @PostMapping("/product/autoScheduleByWoId")
    public ResultModel autoScheduleByWoId(@RequestParam String woId) {
        productManufactInforService.autoScheduleByWoId(woId);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("自动排程By多个工单id")
    @PostMapping("/product/autoScheduleByBatchWoId")
    public ResultModel autoScheduleByWoId(@RequestParam List<String> woIds) {
        productManufactInforService.autoScheduleByWoIds(woIds);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("自动排程By批次id")
    @PostMapping("/product/autoScheduleByBatchPlanId")
    public ResultModel autoScheduleByBatchPlanId(@RequestParam String batchPlanId) {
        productManufactInforService.autoScheduleByBatchPlanId(batchPlanId);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("对多个批次批量自动排程")
    @PostMapping("/product/autoScheduleByBatchPlanIds")
    public ResultModel autoScheduleByBatchPlanIds(@RequestBody List<String> batchPlanIds) {
        productManufactInforService.autoScheduleByBatchPlanIds(batchPlanIds);
        return ResultModel.success(ResultModel.UPDATE);
    }


//    @ApiOperation("对多个工单排程")
//    @PostMapping("/product/scheduleByWoIds")
//    public ResultModel scheduleByWoIds(@RequestBody ScheduleDto scheduleDto) {
//        productManufactInforService.scheduleByWoIds(scheduleDto, true);
//        return ResultModel.success(ResultModel.UPDATE);
//    }

    @ApiOperation("对多个工单的排程时间修改")
    @PostMapping("/product/updatePlanTimesByWoIds")
    public ResultModel updatePlanTimesByWoIds(@RequestBody ScheduleDto scheduleDto) {
        productManufactInforService.scheduleByWoIds(scheduleDto, false);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportProductManufactInfor")
    public void exportProductManufactInfor(HttpServletResponse response, @RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductManufactInforEx> productManufactInforPage = productManufactInforService.getProductManufactInforByPage(pageEntity);
        List<ProductManufactInforEx> productManufactInforExes = productManufactInforPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductManufactInfor接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductManufactInforEx.class).sheet("sheet1").doWrite(productManufactInforExes);
    }

    @ApiOperation("根据工单编号查找工序名称")
    @PostMapping("/product/getProcessNamesByWo")
    public ResultModel getProcessNamesByWo(String wo){
        return ResultModel.success(productManufactInforService.getProcessNamesByWo(wo));
    }
}